﻿/*
 ***************************************************************************
 * Microsoft Visual C# .NET
 * 名称：       App_Input_MenuInfoForm
 * 类型：       
 * 功能说明：   
 * 作者：       谈
 * 创建日期：   2007/11/12
 * 修改日期     修改者      修改内容
 * 
 * 
 * ---
 ***************************************************************************
*/
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Input.Bean;
using Shpm.App.Input.Dao;
using Shpm.App.Master.Bean;
using Shpm.App.Master.Dao;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.System;
using Shpm.App.Commons.Utils;
using System.Reflection;
using Shpm.System.Web.Control;
using log4net;

public partial class App_Input_MenuInfoForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        this.Label1.Text = this.hidWeek.Value;
        if (!IsPostBack)
        {
            initDic();
            
        }
    }

    /*
     * 函数名：initDic
     * 功能：绑定数据字典
     * 参数：无
     * 说明：
     */
    private void initDic()
    {

        CommonDictionaryBean paramBean = new CommonDictionaryBean();
        CommonDictionaryDao dao = new CommonDictionaryDao();
        /*用餐时间*/
        paramBean.Catalog = "M_DISH.DISH_TIME";
        initAll(dao, paramBean, this.DrpTime);
    }
    /*
     * 函数名：initAll
     * 功能：绑定数据字典
     * 参数：无
     * 说明：
     */
    private void initAll(CommonDictionaryDao dao, CommonDictionaryBean paramBean, ListControl obj)
    {
        IList list = null;
        list = dao.FindByCatalog(paramBean);

        obj.DataSource = list;
        obj.DataBind();
    }
    protected void save_Click(object sender, EventArgs e)
    {

        //if (this.ComValidator1.DoValidator() == false)
        //{

        //    ((Design_MasterPage)this.Master).InfoMessage = this.ComValidator1.ErrorMessage;

        //    return;
        //}
        if (ViewState["MENU_DATE"] == null)
        {
            return;
        }
        if (viewSrhRst.Rows.Count <= 0)
        {
            return;
        }

        MenuDao Dao = new MenuDao();
        MenuBean dataBean = new MenuBean();
        dataBean.MenuTime = DrpTime.SelectedValue;
        dataBean.MenuDate = Convert.ToDateTime(ViewState["MENU_DATE"]);
        dataBean = Dao.CheakMenu(dataBean);
        if (dataBean != null)
        {
            //删除数据
            MenuBean delmBean = new MenuBean();
            MenuDetailBean delmdBean = new MenuDetailBean();
            delmBean.MenuId = Convert.ToInt32(dataBean.MenuId);
            delmdBean.MenuId = Convert.ToInt32(dataBean.MenuId);
            Dao.DelMenu(delmBean);
            Dao.DelMenuDetail(delmdBean);
        }
        //插入数据
        MenuBean muBean = new MenuBean();
        muBean.MenuTime = DrpTime.SelectedValue;
        muBean.MenuDate = Convert.ToDateTime(ViewState["MENU_DATE"]);
        int iMenuId = (int)Dao.InsertMenu(muBean);
        saveMenuDetail(iMenuId);
    }
    protected void viewSrhRst_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {

    }
    protected void viewSrhRst_RowDataBound(object sender, GridViewRowEventArgs e)
    {

    }
    protected void btnSeach_Click(object sender, EventArgs e)
    {
        if (this.ComValidator1.DoValidator() == false)
        {

            ((Design_MasterPage)this.Master).InfoMessage = this.ComValidator1.ErrorMessage;

            return;
        }
        if (this.DrpTime.Items.Count == 0)
        {
            ((Design_MasterPage)this.Master).InfoMessage = "请下载最新的数据字典配置文件";
            return;
        }
        this.save.Visible = true;
        this.LabDate.Text = this.txtDate.Text + "(" + this.hidWeek.Value + ")";
        this.LabTime.Text = this.DrpTime.SelectedItem.Text;

        DishDao dao = new DishDao();
        IList lstRst = null;
        DishBean bean = new DishBean();
        lstRst = dao.ListDish(bean);
        viewSrhRst.DataSource = lstRst;
        viewSrhRst.DataBind();

        this.ViewState["MENU_DATE"] = txtDate.Text;

        ChkBoxClear();
        MenuDao Dao = new MenuDao();
        MenuBean dataBean = new MenuBean();
        dataBean.MenuTime = DrpTime.SelectedValue;
        dataBean.MenuDate = Convert.ToDateTime(txtDate.Text);
        dataBean = Dao.CheakMenu(dataBean);

        if (dataBean == null)
        {
            dataBean = new MenuBean();
            dataBean.MenuTime = DrpTime.SelectedValue;
            dataBean.MenuDate = Convert.ToDateTime(txtDate.Text).AddDays(-7);
            dataBean = Dao.CheakMenu(dataBean);
            if (dataBean == null)
            {
                //((Design_MasterPage)this.Master).InfoMessage =
                        //HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0046").ToString();
                return;
            }
        }

        MenuDetailBean mdBean = new MenuDetailBean();
        mdBean.MenuId = Convert.ToInt32(dataBean.MenuId);

        IList dataList = Dao.ListMenuDetail(mdBean);
        if (dataList.Count > 0)
        {
            for (int i = 0; i < dataList.Count; i++)
            {
                MenuDetailBean MenuDBean = new MenuDetailBean();
                MenuDBean = (MenuDetailBean)dataList[i];
                GVselected(Convert.ToString(MenuDBean.DishId));
            }
        }
    }
    private void GVselected(string DishId)
    {
        if (viewSrhRst.Rows.Count > 0)
        {
            for (int i = 0; i < viewSrhRst.Rows.Count; i++)
            {
                if (viewSrhRst.Rows[i].Cells[8].Text == DishId)
                {
                    CheckBox ChkBox = viewSrhRst.Rows[i].Cells[0].Controls[1] as CheckBox;
                    ChkBox.Checked = true;
                }
            }
        }
    }

    private void ChkBoxClear()
    {
        if (viewSrhRst.Rows.Count > 0)
        {
            for (int i = 0; i < viewSrhRst.Rows.Count; i++)
            {
                CheckBox ChkBox = viewSrhRst.Rows[i].Cells[0].Controls[1] as CheckBox;
                ChkBox.Checked = false;
            }
        }
    }

    private void saveMenuDetail(int Id)
    {
        IList lst = new ArrayList();
        MenuDao Dao = new MenuDao();
        SysUserManager manager = new SysUserManager(Context);
        string userId = manager.getSysUserBean().getMstSysUserBean().SysUserId;

        for (int i = 0; i < viewSrhRst.Rows.Count; i++)
        {
            CheckBox ChkBox = viewSrhRst.Rows[i].Cells[0].Controls[1] as CheckBox;
            if (ChkBox.Checked)
            {
                MenuDetailBean mdBean = new MenuDetailBean();
                mdBean.MenuId = Id;
                mdBean.DishId = Convert.ToInt32(viewSrhRst.Rows[i].Cells[8].Text);
                mdBean.UserId = userId;
                lst.Add(mdBean);
            }
        }

        if (Dao.InsertMenuDetailLst(lst, Id) != 0)
        {

                ((Design_MasterPage)this.Master).InfoMessage =
                          HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0001").ToString();
        }
    }
}
